package com.hnucm.onlinealgstudy.controller;

import com.hnucm.onlinealgstudy.pojo.Approval;
import com.hnucm.onlinealgstudy.pojo.Comment;
import com.hnucm.onlinealgstudy.pojo.User;
import com.hnucm.onlinealgstudy.service.CommentService;
import com.hnucm.onlinealgstudy.service.UserService;
import com.hnucm.onlinealgstudy.utils.ResultOBJ;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/oasadapi/comment")
public class CommentController {
@Autowired
    CommentService commentService;
@Autowired
    UserService userService;
// 获取评论
@GetMapping
public List<Comment> getCommentList() {
    return commentService.getCommentList();
}
// 添加评论
@PostMapping
public ResultOBJ addComment(@RequestBody Comment comment){
    try {
        String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
        comment.setPublish_time(date);
        User user =userService.getAllUserList(comment.getAuthor(),null,null).get(0);
        comment.setAuthor_id(user.getId());
        System.out.println(comment.getAuthor_id());
        commentService.addComment(comment);
        return ResultOBJ.ADD_COMMENT_SUCCESS;
    } catch (Exception e) {
        return ResultOBJ.ADD_COMMENT_ERROR;
    }
}
// 审批评论
    @PostMapping(value ="/approval")
    public ResultOBJ approvalComment(@RequestBody Approval approval){
        try {
            commentService.approvalComment(approval);
            return ResultOBJ.APPROVE_COMMENT_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.APPROVE_COMMENT_ERROR;
        }
    }
    // 点赞数加1
    @PostMapping(value ="/{id}")
    public ResultOBJ addLikeNum(@PathVariable Integer id){
        try {
            commentService.addLikeNum(id);
            return ResultOBJ.UPDATE_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.UPDATE_ERROR;
        }
    }
    // 点赞数减1
    @PostMapping(value ="/subnum/{id}")
    public ResultOBJ subLikeNum(@PathVariable Integer id){
        try {
            commentService.subLikeNum(id);
            return ResultOBJ.UPDATE_SUCCESS;
        } catch (Exception e) {
            return ResultOBJ.UPDATE_ERROR;
        }
    }
}
